<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Users as UM;
use app\admin\model\Role;
use app\admin\validate\AdminValidate;

class Users extends Common
{
    private $noCheckAction = ['logout'];//不需要验证的方法
    
    public function initialize(){
        parent::initialize();
        $this->doCheckAuth($this->noCheckAction);
    }
    /**
    * 管理员列表
    */
    public function index()
    {
        $model = new UM();
        $request = $this->request->get();
        $search_where = $model->_where($request);
        $where = $search_where['where'];
        $search = $search_where['search'];
        $data = $model->_list($where);
        $count = $model->_count($where);
        
        $role_model = new Role();
        $roles = $role_model->_getRoles(0);
        
        $this->assign([
            'search' => $search,
            'list' => $data['list'],
            'page' => $data['page'],
            'count' => $count,
            'roles'=>$roles
        ]);
        
        return $this->fetch();
    }

    /**
    * 管理员添加
    */
    public function add(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $validate = new AdminValidate();
            if(!$validate->scene($validate::SCENE_ADD)->check($request)){
                die(outputError($validate->getError()));
            }
                        
            $model = new UM();
            if(!$model->_checkName($request['name'])){
                die(outputError($model->getError()));
            }
            if(!$model->_checkPwd($request['pwd'], $request['pwd2'])){
                die(outputError($model->getError()));
            }
            $ret = $model->_create($request);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '添加成功'));
        }else{
            $model = new Role();
            $roles = $model->_getRoles(0);
            $this->assign(['roles'=>$roles]);
            return $this->fetch();
        }
    }
    
    /**
    * 编辑管理员
    */
    public function edit(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $model = new UM();
            $ret = $model->_modify($request);
            if(!$ret){
                die(outputError($model->getError()));
            }
            die(outputSuccess([], '编辑成功'));
        }else{
            $id = (int)$this->request->get('id');
            //var_dump($request);die;
            $model = new Role();
            $roles = $model->_getRoles(0);
            
            $user_model = new UM();
            $data = $user_model->_findOne($id);
            $this->assign(['roles'=>$roles, 'data'=>$data]);
            return $this->fetch();
        }
    }
    
    /**
    * 删除管理员
    */
    public function del(){
        if($this->request->isAjax()){
            $id = (int)$this->request->post('id');
            $model = new UM();
            $ret = $model->_remove($id);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }
    
    /**
    * 批量删除
    */
    public function delAll(){
        if($this->request->isAjax()){
            $ids = (string)$this->request->post('ids');
            $model = new UM();
            $ret = $model->_removeAll($ids);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }
    
    
    /**
    * 退出
    */
    public function logout(){
        $model = new UM();
        $model->_logout();
        $this->redirect('login/index');
    }
    
    /**
    * 设置管理状态
    */
    public function set_status(){
        $request = $this->request->post();
        //var_dump($request);die;
        $model = new UM();
        $ret = $model->_setStatus($request);
        if(!$ret){
            die(outputError($model->getError()));
        }
        
        die(outputSuccess());
    }
}
